package org.fedex.rg.ui;

import java.util.List;

import org.fedex.rg.R;
import org.fedex.rg.model.Song;
import org.fedex.rg.servises.DownloadServise;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class SongAdapter extends ArrayAdapter<Song> {

	private List<Song> mSongs;
	private DownloadServise mDownloadServise;

	public SongAdapter(Context context, int textViewResourceId,
			List<Song> items, DownloadServise downloadServise) {
		super(context, textViewResourceId, items);
		this.mSongs = items;
		this.mDownloadServise = downloadServise;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.song_row, null);
		}
		final Song song = mSongs.get(position);
		if (song != null) {
			TextView titleText = (TextView) v.findViewById(R.id.song_title);
			TextView artistText = (TextView) v.findViewById(R.id.song_artist);
			if (titleText != null) {
				titleText.setText(song.getTitle());
			}
			if (artistText != null) {
				artistText.setText(song.getArtist());
			}
			ImageButton donloadButton = (ImageButton) v
					.findViewById(R.id.download_button);
			donloadButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mDownloadServise.downloadSong(song);
				}
			});

		}
		return v;
	}
}
